package com.danny.Controller;

import com.danny.mapper.RoomMapper;
import com.danny.mapper.RoomReserveMapper;
import com.danny.pojo.Room;
import com.danny.pojo.RoomReserve;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RoomReserveController {
    @Autowired
    private RoomReserveMapper roomReserveMapper;
    @Autowired
    private RoomMapper roomMapper;

    //根据订单号查询房间信息
    @GetMapping("/queryRoomByReserveNo")
    public Room queryRoomByReverseNo(int no){
        RoomReserve roomReserve=queryRoomReserveByReserveNo(no);
        return roomMapper.queryRoomByRoomNo(roomReserve.getRoomNo());
    }

    //根据任意预订信息查询房间信息的列表
    @GetMapping("/queryRoomByReserve")
    public List<Room> queryRoomByReserve(RoomReserve roomReserve){
        List<RoomReserve> reserveList=queryRoomReserve(roomReserve);
        List<Room> roomList=new ArrayList<>();
        for (RoomReserve reserve:
                reserveList) {
            Room room=roomMapper.queryRoomByRoomNo(reserve.getRoomNo());
            roomList.add(room);
        }
        return roomList;
    }

    //查询所有预订信息的列表
    @GetMapping("/queryAllRoomReserve")
    public List<RoomReserve> queryAllRoomReserve(){
        return roomReserveMapper.queryAllRoomReserve();
    }

    //根据订单号查询预定信息
    @GetMapping("/queryRoomReserveByReserveNo")
    public RoomReserve queryRoomReserveByReserveNo(int no){
        RoomReserve roomReserve=new RoomReserve();
        roomReserve.setReserveNo(no);
        return queryRoomReserve(roomReserve).get(0);
    }

    //根据任意信息查询符合条件的预定信息的列表
    @GetMapping("/queryRoomReserve")
    public List<RoomReserve> queryRoomReserve(RoomReserve roomReserve){
        return roomReserveMapper.queryRoomReserve(roomReserve);
    }

    //分页查询
    @GetMapping("/queryRoomReserveLimit")
    public List<RoomReserve> queryRoomReserveLimit(int page,int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("page",page);
        map.put("pageSize",pageSize);
        return roomReserveMapper.queryRoomReserveLimit(map);
    }

    //有参数的分页查询
    @GetMapping("/queryRoomReserveLimitHasInput")
    public List<RoomReserve> queryRoomReserveLimitHasInput
    (RoomReserve roomReserve,int page,int pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("roomReserve",roomReserve);
        map.put("page",page);
        map.put("pageSize",pageSize);
        return roomReserveMapper.queryRoomReserveLimitHasInput(map);
    }

    //查询所有预定信息条数
    @GetMapping("/roomReserveCount")
    public int roomReserveCount(){
        return roomReserveMapper.roomReserveCount();
    }

    //查询满足参数条件的预定信息列表
    @GetMapping("/roomReserveCountHasInput")
    public int roomReserveCountHasInput(RoomReserve roomReserve){
        return roomReserveMapper.roomReserveCountHasInput(roomReserve);
    }

    //增加预定信息
    @GetMapping("/addRoomReserve")
    public String addRoomReserve(RoomReserve roomReserve){
        List<RoomReserve> list=queryAllRoomReserve();
        for (RoomReserve r:
             list) {
            if(r.getRoomNo()==roomReserve.getRoomNo())
                return "repetitive";
        }
        if(roomReserveMapper.addRoomReserve(roomReserve)==0){
            return "error";
        }
        Room room=new Room();
        room.setState("预定");
        room.setRoomNo(roomReserve.getRoomNo());
        roomMapper.updateRoom(room);
        return "inserted";
    }

    //更新预定信息
    @GetMapping("/updateRoomReserve")
    public String updateRoomReserve(RoomReserve roomReserve){
        RoomReserve checkState=queryRoomReserveByReserveNo(roomReserve.getReserveNo());
        //System.out.println(checkState.getState());
//        if(checkState==null || checkState.getState()==2){
//            return "stateReject";
//        }
        if(roomReserveMapper.updateRoomReserve(roomReserve)==0){
            return "error";
        }
        return "updated";
    }

    //根据身份证号删除预定信息
    @GetMapping("/deleteRoomReserveByGuestId")
    public String deleteRoomReserveByGuestId(String id){
        if(roomReserveMapper.deleteRoomReserveByGuestId(id)==0){
            return "error";
        }
        return "deleted";
    }

    //根据预定编号删除预定信息
    @GetMapping("/deleteRoomReserveByReserveNo")
    public String deleteRoomReserveByReserveNo(int no){
        if(roomReserveMapper.deleteRoomReserveByReserveNo(no)==0){
            return "error";
        }
        return "deleted";
    }
}
